package models

import (
	"errors"
	"github.com/astaxie/beego/orm"
	"time"
)

type (
	BasicModel struct {
		Status  int32     `json:"-"`
		Deleted bool      `json:"-"`
		Created time.Time `orm:"auto_now_add; type(datetime)" json:"-"`
		Updated time.Time `orm:"auto_now; type(datetime)" json:"-"`
	}
)

/*type RobotCallRecord struct {
	Id            int
	CompanyId     int
	LinkmanId     int
	TaskId        int
	RobotId       int
	CallId        string
	Caller        string
	Called        string
	CallStartTime time.Time `orm:"null"`
	CallEndTime   time.Time `orm:"null"`
	RingTime      time.Time `orm:"null"`
	AnswerTime    time.Time `orm:"null"`
	Result        string    `orm:"null"`
	RecordUrl     string    `orm:"null"`
	BillDuration  int
	CallDuration  int
	AgentDuration int
	LastNode      int
	InterestType  string `orm:"null"`
	ScoreTotal    int
}

func InsertNewBills(bills []*RobotCallRecord) error {

	num := len(bills)
	o := orm.NewOrm()
	succNum, err := o.InsertMulti(num, bills)
	if int(succNum) != num || err != nil {
		log.Error("insert to db failed:%v:%v,err:%v", num, succNum, err)
	}

	return err
}

func InsertNewBill(record *RobotCallRecord) error {

	o := orm.NewOrm()
	succNum, err := o.Insert(record)
	if int(succNum) == 0 || err != nil {
		log.Error("insert to db failed:%v,err:%v", succNum, err)
	}

	return err
}

func GetRecord(taskId, pageSize, pageIndex int) ([]*RobotCallRecord, error) {

	var res []*RobotCallRecord

	o := orm.NewOrm()

	num, err := o.QueryTable("RobotCallRecord").Filter("TaskId", taskId).
		Limit(pageSize, pageSize*pageIndex).All(&res)

	if err != nil || num == 0 {
		log.Error("query call record failed:", err)
		return nil, err
	}

	return res, nil
}

func RegCallRecord() {
	orm.RegisterModel(new(RobotCallRecord))
}*/

type IpccStatusCallcenter struct {
	Id             int    `orm:"pk"`
	Name           string //组件名称：固定callcenter
	Addr           string //服务IP
	Port           int    //端口
	Dsid           string
	Tags           string //用于检索
	Version        string //运行版本号
	Status         int    //组件状态：1 服务中,2 升级中,3 下线
	CallNumbers    int    //当前通话数
	TtsStatus      int    //TTS状态：1 服务中,2 升级中,3 下线
	AsrStatus      int    //ASR状态
	FsStatus       int    //FS状态
	ChannelNumbers int    //当前通道数
	Callback       string //http服务 url
	MgrStauts      int    //管理状态
	TransferPort   int    //转接端口
	LastAlive      int64  //上次心跳时间戳
	LastUpdate     time.Time
	FdOpens        int
	CpuUsage       int
	Connects       int
}

func GetCallCenter() (result []IpccStatusCallcenter, errRet error) {

	o := orm.NewOrm()
	o.Using("default")

	// 查询时间点落在任务时间段内
	numbers, err := o.QueryTable("IpccStatusCallcenter").All(&result)
	if numbers == 0 || err != nil {
		if 0 == numbers {
			errRet = errors.New("there is no callcenter info")
		} else {
			errRet = err
		}
	}

	return
}

type IpccStatusKernel struct {
	Id             int    `orm:"pk"`
	Name           string //组件名称：固定cckernel
	Addr           string //服务IP
	Port           int    //端口
	Tags           string //用于检索
	Version        string //运行版本号
	Status         int    //组件状态：1 服务中,/*2 升级中,*/3 下线
	SessionNumbers int    //当前会话数
	Dsid           string
	Callback       string    //http服务 url
	LastAlive      time.Time //上次心跳时间。小写字母开头，json序列化时会忽略此字段
	MgrStatus      int       // 管理状态:1 服务中,2 升级中,3 下线
}

func GetCCkernel() (result []IpccStatusKernel, errRet error) {

	o := orm.NewOrm()
	o.Using("default")

	// 查询时间点落在任务时间段内
	numbers, err := o.QueryTable(new(IpccStatusKernel)).All(&result)
	if numbers == 0 || err != nil {
		if 0 == numbers {
			errRet = errors.New("there is no cckernel info")
		} else {
			errRet = err
		}
	}

	return
}

func RegStatusModel() {
	orm.RegisterModel(new(IpccStatusKernel), new(IpccStatusCallcenter))
}
